#!/bin/sh

# The reference version of this script is maintained in
# ./live-build-config/lingmo-config/common/includes.installer/lingmo-finish-install
#
# It is used in multiple places to finish configuring the target system
# and build.sh copies it where required (in the simple-cdd configuration
# and in the live-build configuration).

configure_sources_list() {
    if grep -q '^deb ' /etc/apt/sources.list; then
	echo "INFO: sources.list is configured, everything is fine"
	return
    fi

    echo "INFO: sources.list is empty, setting up a default one for Lingmo"

    cat >/etc/apt/sources.list <<END
deb [trusted=yes] https://packages-lingmo.simplelinux.cn.eu.org/polaris polaris main contrib non-free

deb http://debian-archive.trafficmanager.net/debian trixie main contrib
# deb-src http://debian-archive.trafficmanager.net/debian trixie main contrib

deb http://debian-archive.trafficmanager.net/debian trixie-updates main contrib
# deb-src http://debian-archive.trafficmanager.net/debian trixie-updates main contrib

deb http://debian-archive.trafficmanager.net/debian trixie-backports main contrib
# deb-src http://debian-archive.trafficmanager.net/debian trixie-backports main contrib

deb https://security.debian.org/debian-security trixie-security main contrib
# deb-src https://security.debian.org/debian-security trixie-security main contrib
END
    apt-get update
}

get_user_list() {
    for user in $(cd /home && ls); do
	if ! getent passwd "$user" >/dev/null; then
	    echo "WARNING: user '$user' is invalid but /home/$user exists" >&2
	    continue
	fi
	echo "$user"
    done
    echo "root"
}

configure_zsh() {
    if grep -q 'nozsh' /proc/cmdline; then
	echo "INFO: user opted out of zsh by default"
	return
    fi
    if [ ! -x /usr/bin/zsh ]; then
	echo "INFO: /usr/bin/zsh is not available"
	return
    fi
    for user in $(get_user_list); do
	echo "INFO: changing default shell of user '$user' to zsh"
	chsh --shell /usr/bin/zsh $user
    done
}

configure_usergroups() {
    # Ensure those groups exist
    addgroup --system kaboxer || true
    addgroup --system wireshark || true

    # adm - read access to log files
    # dialout - for serial access
    # kaboxer - for kaboxer
    # vboxsf - shared folders for virtualbox guest
    # wireshark - capture sessions in wireshark
    lingmo_groups="adm dialout kaboxer vboxsf wireshark"

    for user in $(get_user_list | grep -xv root); do
	echo "INFO: adding user '$user' to groups '$lingmo_groups'"
	for grp in $lingmo_groups; do
	    getent group $grp >/dev/null || continue
	    usermod -a -G $grp $user
	done
    done
}

configure_sources_list
configure_zsh
configure_usergroups
